<?php

/**
  *  This file contains:
  *
	*  string mycurl_crawl_url(string name, string url, string referer);
  *  string mycurl_post_url(string name, string url, string referer, string datatopost);
  *
	*  void write_progress_file(string file, string file);
	*  void login_msnlive(string user, string pwd);
  */

define("G_COOKIE_FILE", "cookies.txt");
define("G_WRITE_PROGRESS", 1);

function mycurl_crawl_url($p_name, $p_url, $p_reffer="") {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_COOKIEJAR, G_COOKIE_FILE);
  curl_setopt($ch, CURLOPT_COOKIEFILE, G_COOKIE_FILE);
  //curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_URL, $p_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_REFERER, $p_reffer);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
 	 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9",
 	 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
  	"Accept-Language: en-us,en;q=0.5",
  	"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
	));

  $ret = curl_exec($ch);
	if(empty($ret))	die(curl_error($ch));
  curl_close($ch);
  write_progress_file("curlout_".$p_name.".html", $ret);
  print $p_name."complete\n";

  return $ret;
}

function mycurl_post_url($p_name, $p_url, $p_reffer, $p_postme) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_COOKIEJAR, G_COOKIE_FILE);
  curl_setopt($ch, CURLOPT_COOKIEFILE, G_COOKIE_FILE);
  //curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_URL, $p_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_REFERER, $p_reffer);
  //curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $p_postme);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
 	 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9",
  	"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
  	"Accept-Language: en-us,en;q=0.5",
  	"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
	));

  $ret = curl_exec($ch);
	if(empty($ret))	die(curl_error($ch));
  curl_close($ch);
  write_progress_file("curlout_".$p_name.".html", $ret);
  print $p_name."complete\n";

  return $ret;
}

function write_progress_file($p_file, $p_data) {
  if(!G_WRITE_PROGRESS)  return;
	print "writing file ${p_file}\n";
  $out = fopen($p_file, "w");
  fwrite($out, $p_data);
  fclose($out);
}

/**
 * Steps:
 * 1 -- GET www.live.com, cos we need asp to gen a fresh login url link/session id (guessing here)
 * 2 -- GET the login page
 * 3 -- find session id (PPFT), find url to send POST data to, send off the user name and password
 *
 * Note: you have to check the output files yourself to see if everything is fine, the code doesn't
 */
function login_msnlive($p_user, $p_pwd) {
	//sanitize
  $p_user = urlencode($p_user);
	//not sure if one should sanitize pwd, but better not

  //begin
  $step1 = mycurl_crawl_url("Step1", "http://www.live.com");

  //look for sign in
  $pos = strpos($step1, "Sign in");
  $pos_end = $pos;
  while($pos--) {
    if(strcmp(substr($step1, $pos, 4), "href")==0) {
      break;
    }
  }
  $pos_start = $pos;
  $step2_url = substr($step1, $pos_start+6, $pos_end-$pos_start-8);

  $step2 = mycurl_crawl_url("Step2", $step2_url, "http://www.live.com");

  //look for posturl
  $pos_start = strpos($step2, "srf_uPost='")+strlen("srf_uPost='");
  $pos_end = strpos($step2, "';", $pos_start);
  $step3_url = substr($step2, $pos_start, $pos_end-$pos_start);

  //look for PPFT
  $pos = strpos($step2, "PPFT");
  $pos_start = strpos($step2, "value=", $pos);
  $pos_end = strpos($step2, "\"", $pos_start+7);
  $step3_ppft = substr($step2, $pos_start+7, $pos_end-$pos_start-7);

  print "logging in ".$p_user."...\n";

  $step3_postfields_fields = array(
  "idsbho=1",
  "PwdPad=IfYouAreReadingThisYouHaveTooMuchFr",
  "LoginOptions=2",
  "CS=",
  "FedState=",
  "PPSX=P",
  "type=11",
  "login=".$p_user,
  "passwd=".$p_pwd,
  "remMe=1",
  "NewUser=1",
  "PPFT=".urlencode($step3_ppft),
  "i1=0",
  "i2=2"
  );
  $step3_postfields_str = implode("&", $step3_postfields_fields);

	print "posting to ${step3_url}\n";
  $step3 = mycurl_post_url("Step3", $step3_url, $step2_url, $step3_postfields_str);
}
?>